Python Program to Find Factorial of a Number
Q. Write a Python program to take input of a number and find the factorial of that number.
Example:
A sample Python program to calculate factorial of a given number. You can also take input of a number from user and calculate the factorial.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # Initialize a number num = 5 # Uncomment below to take input from user #num = int(input("Enter a number: ")) fact = 1 if num < 0: print("Sorry, factorial does not exist for a negative number") elif num == 0: print("The factorial of 0 is 1") else: for i in range(1, num + 1): fact = fact*i print("The factorial of", num, "is", fact) |
Output:
The factorial of 5 is 120